package com.baizhi.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baizhi.annotation.DeleteAnnotation;
import com.baizhi.annotation.InsertAnnotation;
import com.baizhi.dao.BannerDao;
import com.baizhi.entity.Banner;
import com.baizhi.service.BannerService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BannerServiceImpl implements BannerService {
    @Autowired(required = false)
    private BannerDao bannerDao;


    @Override
    @InsertAnnotation
    public Map selectAll(int page, int limit,String bannerOldName) {
        int offset=(page-1)*limit;
        List<Banner> list=bannerDao.selectAll(offset,limit);
        int count=bannerDao.selectCount(bannerOldName);
        Map map=new HashMap();
        map.put("data",list);
        map.put("count",count);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map update(Banner banner) {
        int count=bannerDao.update(banner);
        Map map=new HashMap();
        if(count>0){
            map.put("isUpdate","修改成功");
        }else{
            map.put("isUpdate","修改失败");
        }
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map updatea(Banner banner) {

        int count=bannerDao.updatea(banner);
        Map map=new HashMap();
        if(count>0){
            map.put("isUpdate","修改成功");
        }else{
            map.put("isUpdate","修改失败");
        }
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map insert(Banner banner) {
        int count=bannerDao.insert(banner);
        Map map=new HashMap();
        if(count>0){
            map.put("isInsert",true);
        }else{
            map.put("isInsert",false);
        }
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map delete(int id) {
        int count=bannerDao.delete(id);
        Map map=new HashMap();
        if(count>0){
            map.put("isDelete","删除成功");
        }else{
            map.put("isDelete","删除失败");
        }
        return map;
    }

    @Override
    @InsertAnnotation
    public Banner selectOne(int id) {

        return bannerDao.selectOne(id);
    }

    @DeleteAnnotation
    public Map updates(Banner banner){
        if(banner.getBannerState()==2){
            banner.setBannerState(1);
        }else{
            banner.setBannerState(2);
        }
        Map map=new HashMap();
        int count=bannerDao.updates(banner);




        System.out.println(banner.getBannerState()+"状态");
        System.out.println(banner.getBannerId()+"编号");
        if(count>0){
            map.put("isUpdates","更改成功");
        }else{
            map.put("isUpdates","更改失败");
        }
        return map;
    }

    @Override
    @InsertAnnotation
    public Map selectAlls() {
        List list = bannerDao.selectAlls();

        Workbook workbook= ExcelExportUtil.exportExcel(new ExportParams("轮播图数据","banner"),Banner.class,list);

        try {
            workbook.write(new FileOutputStream("C://Users//小龙//Desktop//新建文件夹//banner.xlsx"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map map=new HashMap();
        map.put("data",list);
        if(map!=null){
            map.put("isxiazai","导出成功");
        }else{
            map.put("isxiazai","导出失败");
        }
        return map ;
    }
}
